package com.albaba.designpattern.decorator.text_002;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

/**
 * @author zx
 * @version 1.0
 * @description 具体组件：抽象文件读取接口的实现类
 * @date 2025/2/20 20:48
 */
public class BaseFileDataLoader implements DataLoader {


    private String filePath;

    public BaseFileDataLoader(String filePath) {
        this.filePath = filePath;
    }

    @Override
    public String read() {
        try {
            String result = FileUtils.readFileToString(new File(filePath), "UTF-8");
            System.out.println("读取文件内容：" + result);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void write(String data) {
        try {
            System.out.println("写入文件内容：" + data);
            FileUtils.writeStringToFile(new File(filePath), data, "UTF-8");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
